//
//  NSData+EnDeCoding.m
//  MFWSdk
//
//  Created by Song Xiaofeng on 13-9-26.
//  Copyright (c) 2013年 Mafengwo Inc. All rights reserved.
//

#import "NSData+EnDeCoding.h"
#import "Base64.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSData (EnDeCoding)

- (NSData*)base64Encode;
{
    return [Base64 encodeData:self];
}

- (NSData*)base64Decode;
{
    return [Base64 decodeData:self];
}

#pragma mark - Only Encoding
/**
 *  md5 encoding
 *
 *  @return lowercase encoding string
 */
- (NSString*)md5Encode;
{
    // Create byte array of unsigned chars
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    
    // Create 16 byte MD5 hash value, store in buffer
    CC_MD5(self.bytes, self.length, md5Buffer);
    
    // Convert unsigned char buffer to NSString of hex values
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x",md5Buffer[i]];
    
    return output;
}

/**
 *  md5 encoding
 *
 *  @return uppercase encoding string
 */
- (NSString*)MD5Encode;
{
    return [[self md5Encode] uppercaseString];
}
@end
